package com.blogsoho.taurus.controller.admin;

import com.blogsoho.taurus.constants.URIMapping;
import com.blogsoho.taurus.vm.ResponsePagerWrapper;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.CustomerVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 16978 on 2017/6/11.
 */
@Controller
@RequestMapping(URIMapping.ADMIN_CUSTOMER)
public class CustomerController extends BaseAdminController {

    @GetMapping("add")
    public String addCustomerView() {
        return "admin/customer_add";
    }

    @GetMapping("edit/{id}")
    public String editCustomerView(Model model, @PathVariable int id) {
        model.addAttribute("customer", customerService.getCustomerById(id));
        return "admin/customer_edit";
    }

    @GetMapping("list")
    public String listCustomerView(Model model) {
        return "admin/customer_list";
    }

    @PostMapping("add")
    @ResponseBody
    public ResponseWrapper<?> addCustomer(CustomerVO customer) {
        return customerService.addCustomer(customer);
    }

    @PostMapping("edit")
    @ResponseBody
    public ResponseWrapper<?> editCustomer(CustomerVO customer) {
        return customerService.updateCustomer(customer);
    }

    @PostMapping("list")
    @ResponseBody
    public ResponseWrapper<?> listCustomer(int start, int length, int draw) {
        ResponsePagerWrapper<?> response = new ResponsePagerWrapper<>();
        response = customerService.getAllCustomer(start, length);
        response.setDraw(draw);
        return response;
    }

    @PostMapping("alert")
    @ResponseBody
    public ResponseWrapper<?> listAlertCustomer(@RequestParam(defaultValue = "3") int day) {
        ResponsePagerWrapper<?> response = new ResponsePagerWrapper<>();
        response = customerService.getAllCustomerByAlertTime(day);
        return response;
    }

}
